import datetime
import random
from function_tool import time_format_transform
import pymysql
import uuid

import clickhouse_connect

connect_info = {
    'port': 8123,
    'host': '192.168.104.243',
    'username': 'default',
    'password': 'bwda2025',
    'database': 'lap'
}
mysql_connect_info = {
    'port': 3306,
    'host': '192.168.2.142',
    'user': 'root',
    'password': 'cI5v4N2Q%a',
    'database': 'lap'
}
mysql_connection = pymysql.connect(**mysql_connect_info)
mysql_cursor = mysql_connection.cursor()
sql = """
        select distinct bag.dept_id, sd.name, bag.dept_parent_id
        from bpm_audit_group bag
        left join system_dept sd on sd.id = bag.dept_id
        where bag.deleted = 0 and bag.dept_id not in (1)
"""
mysql_cursor.execute(sql)
result = mysql_cursor.fetchall()
mysql_cursor.close()
mysql_connection.close()
client = clickhouse_connect.get_client(**connect_info)
column_list = ['id', 'op_time', 'bwd_create_time', 'apply_master_account_name', 'apply_content', 'apply_resource_code',
               'apply_resource_name', 'auth_start_time', 'auth_end_time', 'gold_apply_id', 'coo_master_account_name',
               'auth_mode', 'auth_gold_result', 'approve_opinion', 'event_type', 'log_from', 'jg_apply_master_org_id']
table_name = 'dwd_gold_apply_logs_local'
insert_data = []
master_account = [{'account': 'luffy', 'name': '路飞'}, {'account': 'zoro', 'name': '索隆'},
                  {'account': 'nami', 'name': '娜美'},
                  {'account': 'usopp', 'name': '乌索普'}, {'account': 'sanji', 'name': '山治'},
                  {'account': 'chopper', 'name': '乔巴'},
                  {'account': 'robin', 'name': '罗宾'}, {'account': 'franky', 'name': '弗兰科'},
                  {'account': 'brook', 'name': '布鲁克'}, {'account': 'jinbe', 'name': '甚平'}]
apply_content = ['查询用户信息', '部署审计应用', '查询用户权限信息', '添加用户权限', str(random.randint(1, 1000000)), uuid.uuid4().hex]
auth_gold_result_list = ['0', '1']
length0 = len(result)
length1 = len(master_account)
op_result_list = ['0', '1']
for i in range(1000000):
    index0 = random.randint(0, length0 - 1)
    index1 = random.randint(0, length1 - 1)
    sid = str(random.randint(10 ** 14, 10 ** 16))
    bwd_create_time = time_format_transform.TimeFormatTransform().get_random_date('2025-10-01', '2025-11-01')
    op_time = bwd_create_time
    apply_master_account_name = master_account[index1]['account']
    apply_content = random.choice(apply_content)
    apply_resource_code = 'apply_resource_code' + str(i).zfill(8)
    apply_resource_name = 'apply_resource_name' + str(i).zfill(8)
    auth_start_time = bwd_create_time
    auth_end_time = auth_start_time + datetime.timedelta(seconds=3600)
    gold_apply_id = uuid.uuid4().hex
    coo_master_account_name = master_account[index1]['account']
    auth_mode = '1'
    auth_gold_result = random.choice(auth_gold_result_list)
    approve_opinion = '同意'
    event_type = '1'
    log_from = 'gold_apply'
    jg_apply_master_org_id = str(result[index0][0])
    insert_data.append([sid, op_time, bwd_create_time, apply_master_account_name, apply_content, apply_resource_code,
                        apply_resource_name, auth_start_time, auth_end_time, gold_apply_id, coo_master_account_name,
                        auth_mode, auth_gold_result, approve_opinion, event_type, log_from, jg_apply_master_org_id])
client.insert(table_name, insert_data, column_list)
client.close()
